<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix flex justify-between">
        <el-form class="flex-grow" inline>
          <el-form-item>
            <el-button
              icon="el-icon-plus"
              type="success"
              @click="deBounceWrap(handleAdd)"
            >新增
            </el-button
            >
            <el-button
              icon="el-icon-delete"
              type="danger"
              @click="deBounceWrap(handleDelAll)"
            >批量删除
            </el-button
            >
          </el-form-item>
        </el-form>

        <div class="flex-grow-0">
          <el-select
            v-model="listQuery.status"
            clearable
            placeholder="请选择开启状态"
          >
            <el-option label="开启" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
          <el-button
            class="el-icon-search p-1 text-white text-2xl bg-green-500 cursor-pointer"
            type="primary"
            @click="getList"
          >搜索
          </el-button
          >
        </div>
      </div>

      <el-table :data="data" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" prop="id" width="150px"></el-table-column>
        <el-table-column label="背景图片" prop="img_url">
          <template slot-scope="{ row }">
            <el-popover max-width="420" placement="right" trigger="hover">
              <img
                :src="row.img_url"
                alt=""
                style="max-width: 200px; max-height: 200px"
              />
              <img
                slot="reference"
                :src="row.img_url"
                alt=""
                style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="created_at"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              plain
              size="mini"
              type="primary"
              @click="deBounceWrap(handleEdit, scope.row)"
            >编辑
            </el-button
            >
            <el-button
              icon="el-icon-delete"
              plain
              size="mini"
              type="danger"
              @click="deBounceWrap(handleDel, scope.row)"
            >删除
            </el-button
            >
            <!--
            <el-dropdown trigger="click" style="margin-left: 20px">
              <el-button type="primary"
                >操作<i class="el-icon-arrow-down el-icon--right"></i
              ></el-button>

              <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>不启用</el-dropdown-item>
                <el-dropdown-item
                  @click.native="deBounceWrap(handleDel, scope.row)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown> -->
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
        :limit.sync="listQuery.page_size"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleClear"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="背景图片" label-width="120px" prop="bg_img_url">
          <el-upload
            :before-upload="handleBeforeUploadPicture"

            :http-request="customizeUpload"
            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"
          >
            <img v-if="form.bg_img_url" :src="form.bg_img_url" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="form.status" :label="1">启用</el-radio>
          <el-radio v-model="form.status" :label="0">禁用</el-radio>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button plain type="primary" @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
        >提交
        </el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""
      ><em style="font-style: normal; color: red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { addGiftList, delGiftList, editGiftList, getListApi } from '@/api/roomGift/descriptionManagement'

export default {
  data() {
    return {

      data: [],
      listQuery: {
        status: undefined,
        page: 1,
        page_size: 20
      },
      total: 0,
      titleOptions: {
        add: '新增',
        edit: '编辑'
      },
      title: 'add',
      rowData: {},
      form: {
        bg_img_url: undefined,
        status: undefined
      },
      rules: {
        bg_img_url: [
          { required: true, message: '请上传背景图片', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择开启状态', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      //勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //列表
    getList() {
      let data = JSON.parse(JSON.stringify(this.listQuery))
      if (!this.listQuery.status) {
        data.status = -1
      }
      getListApi(data).then(res => {
        this.data = res.data.data
        this.total = res.data.total
      })
    },
    handleSuccess() {
      this.form.bg_img_url = this.ossLastImgUrl
    },
    handleBeforeUploadPicture(file) {
      const isPic =
        file.type === 'image/jpg' ||
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/webp'

      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPic) {
        this.$message.error('请上传图片!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPic && isLt2M
    },

    //新增
    handleAdd() {
      this.title = 'add'
      this.dialogVisible = true
    },

    // 编辑
    handleEdit(row) {
      this.title = 'edit'
      this.rowData = row

      this.form.status = this.rowData.status
      this.form.bg_img_url = this.rowData.img_url
      this.dialogVisible = true
    },
    handleSumbit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === 'add') {
            let data = {
              bg_img_url: this.form.bg_img_url,
              status: this.form.status
            }

            addGiftList(data).then(res => {
              if (res.code === 200) {
                this.$message.success('提交成功')
                this.getList()
                this.dialogVisible = false
              } else {
              }
            })
          }
          if (this.title === 'edit') {
            let data1 = {
              id: this.rowData.id,
              bg_img_url: this.form.bg_img_url,
              status: this.form.status
            }

            editGiftList(data1).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.getList()
                this.dialogVisible = false
              }
            })
          }
        } else {

          return false
        }
      })
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false
    },
    handleClear() {
      this.$refs['form'].resetFields()
      this.form = {
        bg_img_url: undefined,
        status: undefined
      }
    },

    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDelAll() {
      if (this.multipleSelection.length) {
        /* 存在勾选数据 */
        const ids = this.multipleSelection.reduce(
          (pre, currRow) => {
            pre += `${currRow.id},`
            return pre
          },
          ''
        )
        delGiftList({ ids })
          .then(() => this.$message.success('删除成功！'))
          .catch(console.error)
          .finally(() => /* 刷新数据 */ {
            this.multipleSelection = []
            this.getList()
          })
      } /* 不存在勾选数据 */ else {
        this.$message.warning('请勾选要删除的数据！')
      }
    },
    // 删除
    handleDel(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      delGiftList(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}

/deep/ .el-upload-list__item {
  transition : none !important;
}

/deep/ .el-upload-list--picture {
  max-height : 103px !important;
  overflow   : hidden;
}

/deep/ .el-upload-list--text {
  max-height : 100px !important;
  overflow   : hidden;
}

/deep/ .el-upload-list__item-name {
  white-space : normal !important;
}
</style>
